var GameLayer = cc.LayerColor.extend({
    scoreValue: null,
    bestValue: null,
    gameBoard: null,
    score: 0,
    bestScore: 0,
    _firstTouch: cc.p(0, 0),
    ctor: function (color) {
        this._super(color);
        this._initUI();
        //this.scheduleUpdate();
    },
    _initUI: function () {
        var boardX = cc.winSize.width * 0.04;
        var boardY = cc.winSize.height * 0.85;
        var boardW = cc.winSize.width * 0.92;

        var scoreWidth = cc.winSize.width * 0.5;
        var scoreHeight = cc.winSize.height * 0.1;
        var scoreTop = cc.winSize.height * 0.85;

        var scoreLabel = new cc.LabelTTF("Score", "Arial", cc.winSize.width / 20.0);
        scoreLabel.setFontFillColor(cc.color(117, 110, 104));
        scoreLabel.setPosition(boardX + scoreWidth / 2, scoreTop + scoreHeight);
        this.addChild(scoreLabel, 100);

        this.scoreValue = new cc.LabelTTF("0", "Arial", cc.winSize.width / 12.0);
        this.scoreValue.setFontFillColor(cc.color(117, 110, 104));
        this.scoreValue.setPosition(boardX + scoreWidth / 2, scoreTop + scoreHeight / 2);
        this.addChild(this.scoreValue, 100);

        var bestLabel = new cc.LabelTTF("Best Score", "Arial", cc.winSize.width / 20.0);
        bestLabel.setFontFillColor(cc.color(117, 110, 104));
        bestLabel.setPosition(boardX + boardW * 5 / 7, scoreTop + scoreHeight);
        this.addChild(bestLabel, 100);

        this.bestValue = new cc.LabelTTF("0", "Arial", cc.winSize.width / 12.0);
        this.bestValue.setFontFillColor(cc.color(117, 110, 104));
        this.bestValue.setPosition(boardX + boardW * 5 / 7, scoreTop + scoreHeight / 2);
        this.addChild(this.bestValue, 100);

        this._readBestScore();

        this.gameBoard = new GameBoard(this, boardX, boardY, boardW);

        this._setupKeyboardListener();
        this._setupTouchListener();
    },
    update: function (dt) {
        return;
    },
    _readBestScore: function () {
        var bestScore = cc.sys.localStorage.getItem("bestScore");
        if (bestScore != null) {
            this.bestScore = bestScore;
            this.bestValue.setString(bestScore);
        }

    },
    addScore: function (s) {
        this.score += s;
        this.scoreValue.setString(this.score);
        if (this.score > this.bestScore) {
            this.bestScore = this.score;
            this.bestValue.setString(this.bestScore);
        }
    },
    _saveScore: function () {
        cc.sys.localStorage.setItem("bestScore", this.bestScore);
    },
    _lose: function () {
        this._saveScore();
        cc.director.runScene(new cc.TransitionFade(0.5, new GameOverScene()));
    },
    _setupKeyboardListener: function () {
        if (cc.sys.capabilities.hasOwnProperty('keyboard')) {
            cc.eventManager.addListener({
                event: cc.EventListener.KEYBOARD,
                onKeyReleased: function (key, event) {
                    var target = event.getCurrentTarget();
                    if (target.gameBoard.isFrozen())
                        return;
                    // move
                    if (key == [cc.KEY.w] || key == [cc.KEY.up]) {
                        target.gameBoard.move(DIRECTION.UP);
                    }
                    else if (key == [cc.KEY.a] || key == [cc.KEY.left]) {
                        target.gameBoard.move(DIRECTION.LEFT);
                    }
                    else if (key == [cc.KEY.d] || key == [cc.KEY.right]) {
                        target.gameBoard.move(DIRECTION.RIGHT);
                    }
                    else if (key == [cc.KEY.s] || key == [cc.KEY.down]) {
                        target.gameBoard.move(DIRECTION.DOWN);
                    }
                    // check verdict
                    if (target.gameBoard.judge() == false) {
                        target.gameBoard.setFrozen(true);
                        target.schedule(target._lose, 0, 0, 2);
                    }
                }
            }, this);
        }
    },
    _setupTouchListener: function () {
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ALL_AT_ONCE,
            onTouchesBegan: function (touches, event) {
                var target = event.getCurrentTarget();
                if (target.gameBoard.isFrozen()) return;
                var touch = touches[0];
                target._firstTouch = touch.getLocation();
            },
            onTouchesEnded: function (touches, event) {
                var target = event.getCurrentTarget();
                if (target.gameBoard.isFrozen()) return;
                var touch = touches[0];
                var secondTouch = touch.getLocation();
                var x = secondTouch.x - target._firstTouch.x;
                var y = secondTouch.y - target._firstTouch.y;
                if (Math.abs(x) > Math.abs(y)) {
                    // left or right
                    if (x > 5)
                        target.gameBoard.move(DIRECTION.RIGHT);
                    else if (x < -5)
                        target.gameBoard.move(DIRECTION.LEFT);
                } else {
                    // up or down
                    if (y > 5)
                        target.gameBoard.move(DIRECTION.UP);
                    else if (y < -5)
                        target.gameBoard.move(DIRECTION.DOWN);
                }
                // check verdict
                if (target.gameBoard.judge() == false) {
                    target.gameBoard.setFrozen(true);
                    target.schedule(target._lose, 0, 0, 2);
                }
            }
        }, this);
    }
});

var GameScene = cc.Scene.extend({
    onEnter: function () {
        this._super();
        var layer = new GameLayer(cc.color(251, 248, 239));
        this.addChild(layer);
    }
});

